package com.haoshuang.zhouzhoubang.ui.page.order.scan.writeofforder

import android.content.Context
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.haoshuang.zhouzhoubang.databinding.ActivityWriteOffOrderBinding
import com.haoshuang.zhouzhoubang.model.bean.HexiaoOrderInfoBean
import com.haoshuang.zhouzhoubang.ui.page.order.scan.ScanViewModel
import com.haoshuang.zhouzhoubang.ui.page.order.scan.writeoffresult.WriteOffResultActivity
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.load

/**
 * 核销
 */
class WriteOffOrderActivity : BaseVMActivity<ScanViewModel, ActivityWriteOffOrderBinding>() {

    var writeOffOrderAdapter: WriteOffOrderAdapter? = null

    private var hexiaoOrderInfoBean: HexiaoOrderInfoBean ?=null
    var list = mutableListOf<HexiaoOrderInfoBean.OrderGoodsBean>()

    companion object {

        fun launch(
            context: Context,hexiaoOrderInfoBean: HexiaoOrderInfoBean
        ) {
            context.startPage<WriteOffOrderActivity>( "hexiaoOrderInfoBean" to hexiaoOrderInfoBean)
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "核销订单"
        binding.titleBar.leftImageView().click { finish() }
        writeOffOrderAdapter = WriteOffOrderAdapter(list)
        binding?.recyclerView?.apply {
            layoutManager =
                LinearLayoutManager(this@WriteOffOrderActivity, LinearLayoutManager.VERTICAL, false)
            adapter = writeOffOrderAdapter
        }
        binding?.tvSure?.clickWithTrigger {
            vm.hexiaoOrder(hexiaoOrderInfoBean?.order_id)
        }
    }

    override fun initData() {
        hexiaoOrderInfoBean = intent.getSerializableExtra("hexiaoOrderInfoBean") as HexiaoOrderInfoBean

        hexiaoOrderInfoBean?.order_goods?.let { list.addAll(it) }
        writeOffOrderAdapter?.notifyDataSetChanged()

        binding?.tvOrderId?.text = hexiaoOrderInfoBean?.order_id
        binding?.tvPayTime?.text = hexiaoOrderInfoBean?.pay_time
        binding?.tvPrice?.text = hexiaoOrderInfoBean?.price
    }

    override fun startObserve() {
       /* vm.getHexiaoOrderInfoData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                if (it?.data != null) {


                }

            }
        }*/

        vm.hexiaoOrderData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                WriteOffResultActivity.launch(this@WriteOffOrderActivity,false,msg)
            }
            onSuccess {
                dismissLoading()
                if (it?.data != null) {
                    WriteOffResultActivity.launch(this@WriteOffOrderActivity,true,"核销成功")
                }
            }
        }


    }
}